/*----------------------------------------------------------------------------

	Copyright (c) 2006, Creator of the Cant Project.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	+ Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	+ Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	+ Neither the name of the Creator of the Cant Project nor the names of its
	contributors may be used to endorse or promote products derived from this
	software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------*/

#include	"basictag.hpp"



/**
	Constant.
	
*/
const std::string		EchoTag::STR_ATTRIB_MESSAGE				= "message";


/**
	Constructor.
	
*/
EchoTag::EchoTag(TiXmlElement* a_pElement )
{
	copy(a_pElement );
	
	m_strMessage			= getAttribute(STR_ATTRIB_MESSAGE );
}
		
		
/**
	Copy constructor.

*/
EchoTag::EchoTag(const EchoTag& a_other )
{
	copy(a_other );
	copyEcho(a_other );
}


/**
	Assignment operator.
	
*/
EchoTag&	EchoTag::operator=(const EchoTag& a_other )
{
	copy(a_other );
	copyEcho(a_other );
	
	return	*this;
}


/**
	Copies the 'echo' specific parts of the object.
	
*/
void	EchoTag::copyEcho(const EchoTag& a_other )
{
	m_strMessage			= a_other.m_strMessage;
}


/**
	Clone.
	
*/
Tag*	EchoTag::clone(void ) const
{
	return	new EchoTag(*this );
}


/**
	Executes this tag.
	
	@param		a_project		containing project.
	@param		a_target		containing target.
*/
void	EchoTag::execute(Project& a_project, ProjectTarget& a_target )
{
	Log::write("echo [" + a_project.evaluate(m_strMessage ) + "]" );
}
		
		
/**
	Gives the message.
	
*/
std::string	EchoTag::getMessage(void ) const
{
	return	m_strMessage;
}



//--------------------------------------------------------------------------//



/**
	Constructor.
	
*/
CompileTag::CompileTag(TiXmlElement* a_pElement )
{
	copy(a_pElement );
}
		
		
/**
	Copy constructor.
	
*/
CompileTag::CompileTag(const CompileTag& a_other )
{
	copy(a_other );
}


/**
	Assignment operator.
	
*/
CompileTag&	CompileTag::operator=(const CompileTag& a_other )
{
	copy(a_other );
	
	return	*this;
}


/**
	Clone.
	
*/
Tag*	CompileTag::clone(void ) const
{
	return	new CompileTag(*this );
}


/**
	Executes this tag.
	
	@param		a_project		containing project.
	@param		a_target		containing target.
*/
void	CompileTag::execute(Project& a_project, ProjectTarget& a_target )
{
	Log::write("executing 'CompileTag'" );
}



//--------------------------------------------------------------------------//



/**
	Constructor.
	
*/
LinkTag::LinkTag(TiXmlElement* a_pElement )
{
	copy(a_pElement );
}


/**
	Copy constructor.
	
*/
LinkTag::LinkTag(const LinkTag& a_other )
{
	copy(a_other );
}


/**
	Assignment operator.
	
*/
LinkTag&	LinkTag::operator=(const LinkTag& a_other )
{
	copy(a_other );
	
	return	*this;
}


/**
	Clone.
	
*/
Tag*	LinkTag::clone(void ) const
{
	return	new LinkTag(*this );
}


/**
	Executes this tag.

	@param		a_project		containing project.
	@param		a_target		containing target.
*/
void	LinkTag::execute(Project& a_project, ProjectTarget& a_target )
{
	Log::write("executing 'LinkTag'" );
}


		
//--------------------------------------------------------------------------//


/**
	Auto-instantiator for the <code>BasicCreator</code> class.
	
*/
static BasicCreator			g_creatorBasic;


/**
	Globals for <code>BasicCreator</code> class.

*/
const std::string			BasicCreator::TAG_NAME_ECHO			= "echo";
const std::string			BasicCreator::TAG_NAME_COMPILE		= "compile";
const std::string			BasicCreator::TAG_NAME_LINK			= "link";



/**
	Default constructor.
	
	Registers itself with <code>TagFactory</code>.
*/
BasicCreator::BasicCreator(void )
{
	//MW:	Only register self once...
	static bool				bRegistered			= false;
	if (! bRegistered )
	{
		TagFactory::getInstance().registerCreator(this );
		bRegistered			= true;
	}
}


/**
	Creates a typed <code>Tag</code> object.

	Implementation for the <code>ITagCreator</code> interface.

	@param		a_pElement		xml element.
	@param		a_strName		name of required tag.
	@return		typed <code>Tag</code> object if name is recognised,
				<code>null</code> otherwise.
*/
Tag*	BasicCreator::createTag(TiXmlElement* a_pElement, const std::string& a_strName )
{
	Tag*			tagCreated				= 0x0;

	//MW:	Is the tag-name recognised..?
	if (TAG_NAME_ECHO == a_strName )
	{
		tagCreated				= new EchoTag(a_pElement );
	}
	else if (TAG_NAME_COMPILE == a_strName )
	{
		tagCreated				= new CompileTag(a_pElement );
	}
	else if (TAG_NAME_LINK == a_strName )
	{
		tagCreated				= new LinkTag(a_pElement );
	}

	return	tagCreated;
}
